package com.situ.mvc.controller;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.situ.mvc.entity.Student;
import com.situ.mvc.entity.StudentTeacherBean;
import com.situ.mvc.entity.Teacher;
//  /student/insert.action
@Controller
@RequestMapping(value="/studentTest")
public class StudentControllerTest {
	
	@RequestMapping(value="/getInsertPage")
	public String getInsertPage() {
		System.out.println("StudentController.getInsertPage()");
		//return "/WEB-INF/jsp/student_insert.jsp";
		return "student_insert";
	}
	
	@RequestMapping(value="/insert")
	public ModelAndView insert(Student student) {
		System.out.println(student);
	    //request.setAttribute("student", student);
		//Model域对象放数据，View处理界面
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("student", student);
		//request.getRequestDespatcher("/student_info.jsp").forwar(req,resp);
		//modelAndView.setViewName("/WEB-INF/jsp/student_info.jsp");
		modelAndView.setViewName("student_info");
		return modelAndView;
	}
	
	//默认情况下方法既可以处理get请求也可以处理post请求
	//@RequestMapping(value="/insert1")
	//如果希望这个方法只处理POST请求：method=RequestMethod.POST
	@RequestMapping(value="/insert1", method=RequestMethod.POST)
	public String insert1(Student student, Model model) {
		System.out.println(student);
	    //request.setAttribute("student", student);
		model.addAttribute("student", student);
		//request.getRequestDespatcher("/student_info.jsp").forwar(req,resp);
		return "student_info";
	}
	
	@RequestMapping(value="/insert3", method=RequestMethod.POST)
	public String insert3(String name, Integer age, String gender, Model model) {
		System.out.println("name: " + name);
		System.out.println("age: " + age);
		System.out.println("gender: " + gender);
		
		model.addAttribute("name", name);
		model.addAttribute("age", age);
		model.addAttribute("gender", gender);
		
		return "student_info";
	}
	
	@RequestMapping(value="/insert2", method=RequestMethod.POST)
	public void insert2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		Integer age = Integer.parseInt(request.getParameter("age"));
		String gender = request.getParameter("gender");
		Student student = new Student(name, age, gender);
		//得到Session
		HttpSession session = request.getSession();
		request.setAttribute("student", student);
		request.getRequestDispatcher("/WEB-INF/jsp/student_info.jsp").forward(request, response);
	}
	
	
	
	@RequestMapping(value="/insertStudentAndTeacher")
	public String insertStudentAndTeacher(StudentTeacherBean bean) {
		Student student = bean.getStudent();
		Teacher teacher = bean.getTeacher();
		System.out.println(student);
		System.out.println(teacher);
		
		return "student_info";
	}
	
	@RequestMapping(value="/deleteAll")
	public String deleteAll(Integer[] ids) {
		for (Integer integer : ids) {
			System.out.println(integer);
		}
		System.out.println(Arrays.toString(ids));
		return "student_info"; //转发
	}
	
	// /student/deleteById.action?id=12
	@RequestMapping(value="deleteById")
	public String deleteById(Integer id, Model model) {
		System.out.println("id:" + id);
		// return "student_info";//转发
		//转发到 /student/pageList.action 请求
		//return "forward:/student/pageList.action";
		//response.sendRedirect("/Java1807SpringMVC/student/pageList.action");
		return "redirect:/student/pageList.action";
	}
	
	@RequestMapping(value="/pageList")
	public String pageList() {
		System.out.println("StudentController.pageList()");
		return "student_list";
	}
	
}
